/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2013-2014 sagyf Yang. The Four Group.
 */

package app.models.gift;

import app.models.DaoKit;
import app.models.member.Member;
import com.github.sog.annotation.TableBind;
import com.github.sog.controller.datatables.core.DatatablesCriterias;
import com.github.sog.kit.date.DateProvider;
import com.github.sog.libs.AppFunc;
import com.github.sog.plugin.sqlinxml.SqlKit;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.jfinal.ext.kit.DateKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;
import org.joda.time.DateTime;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * .
 * </p>
 *
 * @author sagyf yang
 * @version 1.0 2014-05-27 22:37
 * @since JDK 1.6
 */
@TableBind(tableName = "ol_gift_exchange")
public class Exchange extends Model<Exchange> {
    /**
     * The public dao.
     */
    public static final Exchange dao = new Exchange();

    private static final long serialVersionUID = 2434556413146575130L;

    public List<Record> findMineByStatus(long mid, boolean status) {
        return Db.find(SqlKit.sql("giftexchange.findMineByStatus"), mid, status);
    }

    public long getCountByMid(int mid) {
        return Db.queryLong("select count(*) from ol_gift_exchange where member=?", mid);
    }

    public boolean exchange(Gift gift, Member member, String voucher) {
        Exchange exchange = new Exchange();
        exchange.set("gift", gift.getNumber(AppFunc.TABLE_PK_COLUMN));
        exchange.set("member", member.getNumber(AppFunc.TABLE_PK_COLUMN));
        exchange.set("integral", gift.getNumber("intergral"));
        exchange.set("voucher", voucher);
        exchange.set("receive", false);
        final Date currentDate = DateProvider.DEFAULT.getCurrentDate();
        exchange.set("exchange_time", currentDate);
        exchange.set("create_time", currentDate);
        exchange.save();

        final String day = new DateTime(currentDate).toString(DateKit.dateFormat);
        //DateKit.toStr(currentDate);
        GiftDialyStat dialyStat = GiftDialyStat.dao.findByDay(day);
        if (dialyStat == null) {
            dialyStat = new GiftDialyStat();
            dialyStat.set("day", day);
            dialyStat.set("frequency", 1);
            dialyStat.set("exchange_ids", exchange.getNumber(AppFunc.TABLE_PK_COLUMN));
            dialyStat.set("create_time", currentDate);
            dialyStat.set("last_exchange_time", currentDate);
            dialyStat.save();
        } else {
            dialyStat.set("frequency", dialyStat.getInt("frequency") + 1);
            dialyStat.set("exchange_ids", dialyStat.getStr("exchange_ids") + exchange.getNumber(AppFunc.TABLE_PK_COLUMN));
            dialyStat.set("last_exchange_time", currentDate);
            dialyStat.update();
        }
        return true;
    }

    public Record findBySnAndQuery(String sn, String query) {
        List<Record> records = Db.find(SqlKit.sql("giftexchange.findBySnAndQuery"), sn, query, query);
        if (!records.isEmpty()) {
            return records.get(0);
        }
        return null;
    }

    public com.jfinal.plugin.activerecord.Page<Record> filterAndList(DatatablesCriterias criterias, String start_time, String end_time, String card_no, String gift) {

        String where = SqlKit.sql("giftexchange.where");
        List<Object> _params = Lists.newArrayList();
        if(!Strings.isNullOrEmpty(start_time)){
            if(Strings.isNullOrEmpty(end_time)) end_time = start_time;
            where += " AND e.exchange_time BETWEEN ? AND ? ";
            _params.add(start_time);
            _params.add(end_time);
        }
        if(!Strings.isNullOrEmpty(card_no)){
            where += " AND m.card_no = ? ";
            _params.add(card_no);
        }
        if(!Strings.isNullOrEmpty(gift)){
            gift += " AND g.name LIKE ? ";
            _params.add(DaoKit.like(gift));
        }
        return DaoKit.paginate(where, SqlKit.sql("giftexchange.columns"), criterias, SqlKit.sql("giftexchange.orders"), _params);

    }
}